package com.pig4cloud.pig.module.controller.product;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.product.ProductRule;
import com.pig4cloud.pig.module.service.product.IProductRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 商品规则值(规格)Controller
 *
 * @author ruoyi
 * @date 2021-08-13
 */
@Validated
@Api(value = "商品规则值(规格)控制器", tags = {"商品规则值(规格)管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/product/rule")
public class ProductRuleController {

    private final IProductRuleService productRuleService;

    /**
     * 查询商品规则值(规格)列表
     */
    @ApiOperation("查询商品规则值(规格)列表")
	@PreAuthorize("@pms.hasPermission('module:productrule:list')")
    @GetMapping("/list")
    public TableDataInfo<ProductRule> list(@Validated ProductRule productRule) {
        QueryWrapper<ProductRule> qw = new QueryWrapper<>();
        if (productRule.getMerId()!=null){
            qw.eq("mer_id",productRule.getMerId());
        }
		TableDataInfo<ProductRule> td = PageUtils.buildDataInfo(productRuleService.page(PageUtils.buildPage(),qw));
		List<ProductRule> list = td.getData();
		list.forEach(rule->{
			if(StrUtil.isNotEmpty(rule.getRuleValue())){
				rule.setRuleValueArray(JSONUtil.parseArray(rule.getRuleValue()));
			}
		});
        return td;
    }


    /**
     * 查询商品规则值(规格)列表
     */
    @ApiOperation("查询商品规则值(规格)列表")
    @GetMapping("/listNoPage/{ruleId}")
    public R listNoPage(@PathVariable("ruleId") Integer ruleId) {
        List<ProductRule> list = productRuleService.getRuleListByCateChildId(ruleId);
        list.forEach(rule->{
            if(StrUtil.isNotEmpty(rule.getRuleValue())){
                rule.setRuleValueArray(JSONUtil.parseArray(rule.getRuleValue()));
            }
        });
        return R.ok(list);
    }

    /**
     * 获取商品规则值(规格)详细信息
     */
    @ApiOperation("获取商品规则值(规格)详细信息")
	@PreAuthorize("@pms.hasPermission('module:productrule:query')")
    @GetMapping("/{id}")
    public R<ProductRule> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Integer id) {
		ProductRule productRule = productRuleService.getById(id);
		if(productRule!=null){
			if(StrUtil.isNotEmpty(productRule.getRuleValue())){
				productRule.setRuleValueArray(JSONUtil.parseArray(productRule.getRuleValue()));
			}
		}
        return R.ok(productRule);
    }

    /**
     * 新增商品规则值(规格)
     */
    @ApiOperation("新增商品规则值(规格)")
	@PreAuthorize("@pms.hasPermission('module:productrule:add')")
    @SysLog("商品规则值(规格)")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody ProductRule productRule) {
		if(productRule.getRuleValueArray().size() > 0){
			productRule.setRuleValue(JSONUtil.toJsonStr(productRule.getRuleValueArray()));
		}
        return R.okByRow(productRuleService.save(productRule) ? 1 : 0);
    }

    /**
     * 修改商品规则值(规格)
     */
    @ApiOperation("修改商品规则值(规格)")
    @SysLog("商品规则值(规格)")
	@PreAuthorize("@pms.hasPermission('module:productrule:edit')")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody ProductRule productRule) {
		if(productRule.getRuleValueArray().size() > 0){
			productRule.setRuleValue(JSONUtil.toJsonStr(productRule.getRuleValueArray()));
		}
        return R.okByRow(productRuleService.updateById(productRule) ? 1 : 0);
    }

    /**
     * 删除商品规则值(规格)
     */
    @ApiOperation("删除商品规则值(规格)")
	@PreAuthorize("@pms.hasPermission('module:productrule:remove')")
    @SysLog("商品规则值(规格)")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Integer[] ids) {
        return R.okByRow(productRuleService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
